﻿/*
	Copyright 2008 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
/*
	Main Functions
*/

/***********************************************************************
 Initialize Gadget
***********************************************************************/
function Init() {
	info.size = "8"

	COMM = new comm();

	pluginHelper.SetFlags( 0, gddContentFlagManualLayout);
	pluginHelper.onAddCustomMenuItems = onAddCustomMenuItems;
	
	options.putDefaultValue("refreshMinutes", "30") 
	options.putDefaultValue("markread", "1") 
	options.putDefaultValue("opacity", "255") 
	options.putDefaultValue("maxitems", "999") 

	divmain.opacity = parseInt( options.getValue( "opacity" ));
  if( divmain.opacity == 255 )
		divmain.background="#FFFFFF"

	loadFeeds();
}

/***********************************************************************
 Get Feed for URL
***********************************************************************/
function getFeed( url ){
	var feed;
	var feeds = restoreFeeds();

	for( var i = 0; i < feeds.length; i ++ )
		if( feeds[i].url == url )
		{
			feed = feeds[i];
			break;
		}
	return feed;
}


/***********************************************************************
 Right clikc on view menu
***********************************************************************/
function itemRightClick(i){
	detailsView = new DetailsView();
  
  detailsView.SetContent( "", undefined,  "menu.xml",  false, 0 );
  detailsView.detailsViewData.putValue( "closeDetailsView", closeMenuView);
	detailsView.detailsViewData.putValue( "item", i );
  plugin.ShowDetailsView(detailsView,  "",  gddDetailsViewFlagNone, onMenuViewFeedback);  
}

/***********************************************************************
  Hide and destroy the currently visible details view
***********************************************************************/
function closeMenuView() {
  plugin.CloseDetailsView();
}

/***********************************************************************
  React to the closure of the menu view.
***********************************************************************/
function onMenuViewFeedback(detailsViewFlags) {
	var itemIndex = detailsView.detailsViewData.getValue("item");
	var action = detailsView.detailsViewData.getValue("action");

	switch(action){
		case 0:
			items[itemIndex].read = true;
			break;
		case 1:
			markRead( itemIndex, false );
			break;
	case 2:
			markRead( itemIndex, true );
			break;
	}
	showItems();
}

/***********************************************************************
 Mark entries as Reed
***********************************************************************/
function markRead( itemIndex, allFeeds ){
	var	url = items[itemIndex ].feedurl;
	var d = Date.parse(Date());
	var feeds = restoreFeeds();

	for( var  i = 0;i < feeds.length;i++)
	{
		if (( allFeeds) || (feeds[i].url == url ))
			feeds[i].lastread = d;
	}
	saveFeeds( feeds );

	for( var i=0; i < items.length; i++ )
	{
		if( allFeeds ) 
			items[i].read = true
		else
			if( items[i].feedurl == url )
				items[i].read = true;
	}
}

/***********************************************************************
 Doubleclick on entry and open in browser
***********************************************************************/
function itemDoubleClick( i, url ){

	var winShell = new ActiveXObject("Shell.Application");
  winShell.ShellExecute( url ); 
	items[i].read = (options.getValue( "markread" ) == "1");
	
	return false;
}

/***********************************************************************
 Clean Cache
***********************************************************************/
function cleanupCache(){
}

/***********************************************************************
 Add Item 
***********************************************************************/
function addItem( item ){
var found = false;

	for(i=0; i < items.length; i++)
	{
		if( items[i].hash == item.hash )
		{
			found = true;
			break;
		}
	}
	if( ! found )
		items.push( item );
}

/***********************************************************************
	Change menu
***********************************************************************/
function onAddCustomMenuItems(menu) 
{
  menu.AddItem(UPDATE_CHECK, 0, runcheckupdate);
  function runcheckupdate() { checkUpdate(true) }

  menu.AddItem(ADD_FEED, 0, runaddfeed);
  function runaddfeed() { addFeed(-1) }
  menu.AddItem(CONFIGURE_FEED, 0, runconfigurefeed);
  function runconfigurefeed() { configureFeeds() }
}

/***********************************************************************
	Add Feed Menu
***********************************************************************/
function addFeed( nr ){
	var feeds = restoreFeeds();

	detailsView = new DetailsView();
  detailsView.SetContent( "", undefined,  "addfeed.xml",  false, 0 );
	detailsView.detailsViewData.putValue( "closeAddView", closeAddView);
	detailsView.detailsViewData.putValue("feedNo", nr );
  if( nr >= 0)
	{
		detailsView.detailsViewData.putValue("url", feeds[nr].url );
		detailsView.detailsViewData.putValue("user", feeds[nr].user );
		detailsView.detailsViewData.putValue("password", feeds[nr].password );
		detailsView.detailsViewData.putValue("loginurl", feeds[nr].login );
	}
  plugin.ShowDetailsView(detailsView,  "",  gddDetailsViewFlagNone, onAddViewFeedback);  

}

/***********************************************************************
	Close Add Feed View
***********************************************************************/
function closeAddView() {
  plugin.CloseDetailsView();
}

/***********************************************************************
 React to the closure of the addFeed view.
***********************************************************************/
function onAddViewFeedback(detailsViewFlags) {	
	var feed = new Object();
	var feedNo;
	var feeds = restoreFeeds();
	
	feedNo = detailsView.detailsViewData.getValue("feedNo");
	if( feedNo >-1 ) 
	{
		feeds[feedNo].url = detailsView.detailsViewData.getValue("url");
		feeds[feedNo].user = detailsView.detailsViewData.getValue("user");
		feeds[feedNo].password = detailsView.detailsViewData.getValue("password");
		feeds[feedNo].login = detailsView.detailsViewData.getValue("loginurl");
	}
	else
	{
		feed.lastread = 0;
		feed.url = detailsView.detailsViewData.getValue("url");
		feed.user = detailsView.detailsViewData.getValue("user");
		feed.password = detailsView.detailsViewData.getValue("password");
		feed.login = detailsView.detailsViewData.getValue("loginurl");
		if( feed.url > '' )
		{
			feeds.push( feed );
		}
	}
	saveFeeds( feeds );
	loadFeeds();

}

/***********************************************************************
 Configure Feed Menu
***********************************************************************/
function configureFeeds( ){
	var feeds = restoreFeeds();

	detailsView = new DetailsView();
	
  detailsView.SetContent( "", undefined,  "configure.xml",  false, 0 );
	detailsView.detailsViewData.putValue( "closeConfigureView", closeConfigureView);
  plugin.ShowDetailsView(detailsView,  "",  gddDetailsViewFlagNone, onConfigureViewFeedback);  

}

/***********************************************************************
	Close Add Feed View
***********************************************************************/
function closeConfigureView() {
  plugin.CloseDetailsView();
}

/***********************************************************************
 React to the closure of the configure Feed view.
***********************************************************************/
function onConfigureViewFeedback(detailsViewFlags) {	
	var url = "";
	var feedNo = -1;
	var feeds = restoreFeeds();

	view.setTimeout( "loadFeeds()", 1000);

	url = options.getValue("url");

	if(url.length > 0 )
	{
		for( var i = 0; i < feeds.length; i++)
			if(feeds[i].url == url )
			{
				feedNo = i;
				break;
			}
		if( feedNo >-1)
			addFeed( feedNo );
		options.putValue( "url", "" )
	}

	divmain.opacity = parseInt( options.getValue( "opacity" ));
	if( divmain.opacity == 255 )
		divmain.background="#FFFFFF"
}

/***********************************************************************
	Close HTML Detail view
***********************************************************************/
function closeDetailsView() {
  plugin.CloseDetailsView();
}

/***********************************************************************
	return from version view
***********************************************************************/
function onVersionViewFeedback(detailsViewFlags) {
}

/***********************************************************************
	Check for Updates
***********************************************************************/
function checkUpdate(manualrequest)
{
	var today = new Date();

	if( !manualrequest)
	{
		var lastcheck = options.getValue("lastcheck");
		if( lastcheck + 86400000 >  today.getTime())
				return;
	}
	else
		view.setTimeout( "checkUpdate(false)", 86400000);		// Come back one day later

	var version = '';
	var parser = COMM.sendXMLHttpRequest( "http://www.catsoft.ch/gg/rssversioninfo.xml" );

	if (parser)
	{
		if( parser.checkError(false).length == 0 ) 
		{
			version = parser.getValue("version");
			if(version > GADGET_VERSION)
			{
				detailsView = new DetailsView();
		
				detailsView.SetContent(  "", undefined,  "newversion.xml",  false, 0 );
				detailsView.detailsViewData.putValue( "closeDetailsView", closeDetailsView);
				detailsView.detailsViewData.putValue( "version", version);
				detailsView.detailsViewData.putValue( "description", parser.getValue("description"));
				detailsView.detailsViewData.putValue( "link", parser.getValue("link"));
				plugin.ShowDetailsView(detailsView,  "",  gddDetailsViewFlagNone, onVersionViewFeedback);  
			}
				//if there is no update, and the user has manually asked tell them
			else if(manualrequest) //Once again, this will be explained later.
			{
				alert(CURRENT_VERSION);
			}

			options.putValue("lastcheck", today.getTime());
		}
	}
}

/***********************************************************************
	Close Version view
***********************************************************************/
function onVersionViewFeedback(detailsViewFlags) {
}

/***********************************************************************
	Load data on changes
***********************************************************************/
function view_onOptionsChanged(){

}
